/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.context.support;

import java.util.Iterator;
import java.util.Map;

import com.bluemartini.dna.DNAList;
import com.bluemartini.dna.DNAListArray;
import com.bluemartini.dna.DynamicDomainListener;

/**
 * Provides a dynamic domain listener adapter to simplify dynamic domain
 * management.
 *  
 * @author Scott Rossillo
 *
 */
public class DynamicDomainListenerAdapter
		extends AbstractDynamicDomainListener
		implements DynamicDomainListener {
	
	/**
	 * Creates a new dynamic domain adapter.
	 */
	public DynamicDomainListenerAdapter() { }
	
	/**
	 * Creates a new dynamic domain adapter supporting
	 * a single domain.
	 * 
	 * @param domainName the name of the dynamic domain
	 * this dynamic domain listener supports
	 */
	public DynamicDomainListenerAdapter(String domainName) {
		super(domainName);
	}
	
	/**
	 * Creates a new dynamic domain adapter supporting
	 * all the given domains.
	 * 
	 * @param domainNames an array of domain names this
	 * this dynamic domain listener supports
	 */
	public DynamicDomainListenerAdapter(String[] domainNames) {
		super(domainNames);
	}
	
	/* (non-Javadoc)
	 * @see net.martinimix.context.support.AbstractDynamicDomainListener#getDomainValues(java.lang.String)
	 */
	public final DNAListArray getDomainValues(String domainName) {
		return loadDomainValues(domainName);
	}

	/**
	 * Creates a new dynamic domain entry from the given key and value pair.
	 * 
	 * @param key the <code>code</code> value for the domain entry
	 * @param value the <code>decode</code> value for the domain entry
	 * 
	 * @return a <code>DNAList</code> containing <code>code</code> and
	 * <code>decode</code> domain values provided by the given
	 * <code>key</code> and <code>value</code> respectively
	 */
	protected final DNAList createDomainEntry(String key, String value) {
		
		DNAList entry = new DNAList();
		
		entry.setString("code", key);
		entry.setString("decode", value);
		
		return entry;
	}
	
	/**
	 * Returns the domain values for the given domain name.  Subclasses
	 * may prefer to override the simpler {@link #loadDomainValuesMap(String)} method
	 * instead as this implementation relies on {@linkplain #loadDomainValuesMap(String)}
	 * to provide domain values.
	 * 
	 * @param domainName the name of the domain whose domain values
	 * should be returned
	 * 
	 * @return a <code>DNAListArray</code> containing the domain values
	 * for the given <code>domainName</code>; may not be <code>null</code>
	 * 
	 * @see #loadDomainValuesMap(String)
	 */
	public DNAListArray loadDomainValues(String domainName) {
		
		final Map domainValueMap = loadDomainValuesMap(domainName);
		final DNAListArray domainValues = new DNAListArray();
		String key;
		
		for(Iterator it = domainValueMap.keySet().iterator(); it.hasNext(); ) {
			key = (String) it.next();
			domainValues.addElementRef(createDomainEntry(key, (String) domainValueMap.get(key)));
		}
		
		return domainValues;
	}
	
	/**
	 * Returns a map containing the domain values for the given domain name.
	 * 
	 * @param domainName the name of the dynamic domain whose domain values should
	 * be returned.
	 * 
	 * @return a <code>Map</code> containing the domain values for the given
	 * <code>domainName</code>
	 */
	public Map loadDomainValuesMap(String domainName) {
		return null;
	}
	
}
